package com.bit.stack;

public class MyStack {
    private String[] arr;
    private int size;
    public MyStack(){
        arr=new String[1000];
        size=0;
    }
    public MyStack(int capacity){
        arr=new String[capacity];
        size=0;
    }

    //入栈
    //elem就是element的缩写
    public void push(String elem){
        if (size== arr.length){
            resize();
        }
        arr[size]=elem;
        size++;
    }

    //出栈
    public String pop() {
        if (size==0){
            throw new RuntimeException("栈为空");
        }
        String elem=arr[size-1];
        size--;
        return elem;
    }

    public String peek(){
        if (size==0){
            throw new RuntimeException("栈为空");
        }
        String elem=arr[size-1];
        return elem;
    }
    public void resize(){
        //创建一个更长的数组
        String[] newArr=new String[arr.length*2];
        //把原有的元素复制到新数组中
        for (int i=0;i< arr.length;i++){
            newArr[i]=arr[i];
        }
        //3.把新数组赋值给原数组
        arr=newArr;
    }

    public static void main(String[] args) {
        MyStack stack=new MyStack();
        stack.push("aaa");
        stack.push("bbb");
        stack.push("ccc");
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());

        System.out.println(stack.pop());
    }
}
